//
//  TWVideoCollectionView.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/3/17.
//  Copyright © 2017年 个人. All rights reserved.
//

import UIKit

let videoCollectionViewCellId = "videoCollectionViewCell"

class TWVideoCollectionView: UICollectionView {
    var indexItem : IndexPath?
    
    var videoArray : [TWItemModel]?{
        didSet {
            guard videoArray != nil else {
                return
            }
//            reloadData()
        }
    }
    var item : TWItemModel?{
        didSet {
            guard let item = item else {
                return
            }
            // 拿到索引
            for one in videoArray! {
                if one.isEqual(item) {
                    let index : Int = videoArray!.index(of: item)!
                    let indextP = IndexPath(item: index, section: 0)
//                    layoutIfNeeded()
                    reloadData()
                    // 刷新完成之后滚动到当前显示的cell
                    performBatchUpdates(nil, completion: { (_) in
                        self.scrollToItem(at: indextP, at: .left, animated: false)
                    })
                    break
                }
            }
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
//        showsVerticalScrollIndicator = false
//        showsHorizontalScrollIndicator = false
//        delegate = self
//        dataSource = self
//        register(UINib(nibName: "TWVideoCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: videoCollectionViewCellId)
    }
}

// MARK:- ——————代理方法——————
// MARK:- collectionView的代理
//extension TWVideoCollectionView : UICollectionViewDataSource, UICollectionViewDelegate {
//    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        return videoArray!.count
//    }
//    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: videoCollectionViewCellId, for: indexPath) as! TWVideoCollectionViewCell
//        cell.cellModel = videoArray![indexPath.item]
//        return cell
//    }
//    func scrollViewDidScroll(_ scrollView: UIScrollView) {
//        TWLog(message: scrollView.contentOffset.x)
//    }
//}
